package com.youyou.fusheng2.View.Fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.youyou.fusheng2.Moudle.FuShengCallBack;
import com.youyou.fusheng2.Moudle.OnClick2;
import com.youyou.fusheng2.Moudle.SouSuoImageCliCk;
import com.youyou.fusheng2.Moudle.sousuoShow;
import com.youyou.fusheng2.Person.FuShengMoudle;
import com.youyou.fusheng2.Person.Test.HomeBannerTest;
import com.youyou.fusheng2.Person.Test.HomeTedayTest;
import com.youyou.fusheng2.R;
import com.youyou.fusheng2.Utils.EveryUrl;
import com.youyou.fusheng2.View.Activity.FourButtonActivity;
import com.youyou.fusheng2.View.Adapter.HomeAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HomeFragment extends Fragment implements View.OnClickListener, FuShengCallBack, OnLoadMoreListener, sousuoShow, OnClick2 {

    private RecyclerView              home_recyclerView;
    private ImageView                 home_sousuo;
    private SmartRefreshLayout        home_Refresh;
    private MyHander                  myHander      = new MyHander(); //重写Hander
    private HomeAdapter               homeAdapter;
    private FuShengMoudle             fuShengMoudle = new FuShengMoudle(this);
    private int                       type          = 1;
    private List<String>              bannerTests;
    private List<String>              bannerTestss;
    private List<Map<String, String>> typeTestLists = new ArrayList<>();
    private List<Map<String, String>> LikeTestLists = new ArrayList<>();
    private SouSuoImageCliCk          onClickListner;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.home_fragment, null);
        initview(inflate);
        initrecyView(inflate);
        GetBanner(1);
        return inflate;
    }

    private void GetBanner(int flage) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  20:24
         *  @简介: 获取banner的轮播图
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (flage == 1) {
                    fuShengMoudle.getUrlDate(EveryUrl.bannerUrl, 0);
                    fuShengMoudle.getUrlDate(EveryUrl.vdioType, 1);
                    fuShengMoudle.getUrlDate(EveryUrl.vdioLike , 2);
                } else {
                    fuShengMoudle.getUrlDate(EveryUrl.vdioLike, 2);
                }
            }
        }).start();
    }

    private void initrecyView(View inflate) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  18:19
         *  @简介: 初始化recyclerView
         */
        homeAdapter = new HomeAdapter(this, (OnClick2) this);
        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        home_recyclerView.setLayoutManager(gridLayoutManager);
        home_recyclerView.setAdapter(homeAdapter);
    }

    private void initview(View inflate) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  18:17
         *  @简介: 初始化布局
         */
        home_sousuo = inflate.findViewById(R.id.home_sousuo);
        home_sousuo.setOnClickListener(this);
        TextView home_tetle = inflate.findViewById(R.id.home_tetle);
        home_recyclerView = inflate.findViewById(R.id.home_recyclerView);
        home_recyclerView.getItemAnimator().setChangeDuration(0);
        home_sousuo.setOnClickListener(this);
        home_tetle.setText("浮生影视");
        home_Refresh = inflate.findViewById(R.id.home_Refresh);
        home_Refresh.setOnLoadMoreListener(this);
        home_Refresh.setEnableRefresh(false);
        home_Refresh.setEnableOverScrollDrag(true);
        home_Refresh.setEnableOverScrollBounce(true);
        home_Refresh.setEnableFooterTranslationContent(true);
        home_Refresh.setEnableAutoLoadMore(true);
    }

    @Override
    public void onClick(View view) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  19:39
         *  @简介: 控件监听事件
         *  fragment  跳转到  fragment
         */
        if (view.getId() == R.id.home_sousuo) {
            if (onClickListner != null) {
                onClickListner.OnImageCliCk(home_sousuo);
            }
        }
    }

    @Override
    public void GetSuccess(String text, int mark) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  20:18
         *  @简介: 请求成功
         */
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @Override
    public void GetError(String text, int mark) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  20:18
         *  @简介: 请求失败
         */
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/23  17:37
         *  @简介: 刷新上拉
         */
        if (type < 60) {
            GetBanner(2);
            this.type += 17;
            home_Refresh.finishLoadMore(2000);
        } else {
            //设置停止上拉刷新 并设置为没有更多数据
            home_Refresh.finishLoadMoreWithNoMoreData();
        }
    }


    @Override
    public void showTextView(Boolean s) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/27  00:36
         *  @简介: 如果集合为空 回掉一个s
         *  当s为空的true的时候 进行开启定时器
         */

    }


    private void intentBudle(Class c, String s, int i) {
        Bundle bundle = new Bundle();
        bundle.putString("s", s);
        bundle.putInt("f", i);
        getContext().startActivity(new Intent(getContext(), c).putExtra("text", bundle));
    }

    @Override
    public void listener1(int Flage, String text) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/29  16:05
         *  @简介: 参数1是标记
         *  参数2 是回掉的内容
         *  两者都是跳转到 playVdioActivity
         */
        switch (Flage) {
            case 0:
            case 1:
            case 2:
                intentBudle(com.youyou.fusheng2.View.Activity.playVdioActivity.class, null, Integer.parseInt(text));
                break;
        }

    }

    @Override
    public void listener1(int Flage, String Text, int Index) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/27  01:44
         *  @简介: 四个button的点击事件
         *  第一个参数是标记
         *  第二个参数是获取radioButton的文字
         *  第三个参数是点击跳转到另一个页面加载哪个tab
         */
        switch (Flage) {
            case 0:
                intentBudle(FourButtonActivity.class, Text, Index);
                break;
        }
    }

    @SuppressLint("HandlerLeak")
    public class MyHander extends Handler {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    BannerJiexi(msg.obj.toString());
                    break;
                case 1:
                    ListBanner(msg.obj.toString());
                    break;
                case 2:
                    LikeItem(msg.obj.toString());
                    setdata();
                    break;
            }
        }

        private void BannerJiexi(String toString) {
            /**
             *  @作者: $浮生-qq:2567294605$
             *  @时间: 2020/8/17  20:37
             *  @简介: 解析home页面的banner轮播图
             */
            Gson gson = new Gson();
            bannerTests = new ArrayList<>();
            bannerTestss = new ArrayList<>();
            HomeBannerTest homeBannerTest = gson.fromJson(toString, HomeBannerTest.class);
            if (homeBannerTest.getCode() == 0) {
                List<HomeBannerTest.DataBean> dataBeanList = homeBannerTest.getData();
                for (int i = 0; i < dataBeanList.size(); i++) {
                    bannerTests.add(dataBeanList.get(i).getV_pic());
                    bannerTestss.add(String.valueOf(dataBeanList.get(i).getV_id()));
                }
            }
        }

        private void LikeItem(String toString) {
            /**
             *  @作者: $浮生-qq:2567294605$
             *  @时间: 2020/8/17  20:37
             *  @简介: 解析home页面的底部每个item
             */
            Gson gson = new Gson();
            HomeTedayTest homeTedayTest = gson.fromJson(toString, HomeTedayTest.class);
            if (homeTedayTest.getCode() == 0) {
                List<HomeTedayTest.DataBean> data = homeTedayTest.getData();
                for (int i = 0; i < data.size(); i++) {
                    Map<String, String> likeTestmap = new HashMap<>();
                    likeTestmap.put("pic", data.get(i).getV_pic());
                    likeTestmap.put("tid", String.valueOf(data.get(i).getV_id()));
                    likeTestmap.put("tname", String.valueOf(data.get(i).getV_name()));
                    LikeTestLists.add(likeTestmap);
                }
            }
        }

        private void ListBanner(String toString) {
            /**
             *  @作者: $浮生-qq:2567294605$
             *  @时间: 2020/8/17  20:37
             *  @简介: 解析home页面的banner轮播图
             */
            Gson gson = new Gson();
            HomeTedayTest homeTedayTest = gson.fromJson(toString, HomeTedayTest.class);
            if (homeTedayTest.getCode() == 0) {
                List<HomeTedayTest.DataBean> data = homeTedayTest.getData();
                for (int i = 0; i < data.size(); i++) {
                    Map<String, String> typeTestmap = new HashMap<>();
                    typeTestmap.put("pic", data.get(i).getV_pic());
                    typeTestmap.put("tid", String.valueOf(data.get(i).getV_id()));
                    typeTestmap.put("tname", String.valueOf(data.get(i).getV_name()));
                    typeTestLists.add(typeTestmap);
                }
            }
        }

        private void setdata() {
            /**
             *  @作者: $浮生-qq:2567294605$
             *  @时间: 2020/8/20  20:16
             *  @简介: 把集合设置到适配器
             */
            homeAdapter.setBannerTests(bannerTests, bannerTestss);
            homeAdapter.setTypeTests(typeTestLists);
            homeAdapter.setLikeTests(LikeTestLists);
            homeAdapter.notifyItemChanged(3, LikeTestLists.size());
        }
    }


    public void setOnClickListner(SouSuoImageCliCk onClickListner) {
        this.onClickListner = onClickListner;
    }


    @Override
    public void onPause() {
        GetBanner(2);
        super.onPause();
    }
}

